package com.woniuxy.controller;

import com.woniuxy.pojo.CustLost;
import com.woniuxy.pojo.Customer;
import com.woniuxy.pojo.Customer;
import com.woniuxy.pojo.EnertyMy;
import com.woniuxy.service.CustomerService;
import com.woniuxy.service.EnertyMyService;
import com.woniuxy.service.MyLog;
import com.woniuxy.service.impl.CustomerServiceImpl;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.CustLostVo;
import com.woniuxy.vo.CustomerVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class CustmoerController {
    private Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private EnertyMyService enertyMyService;
   @Autowired
   private CustomerService customerService;
   @RequestMapping("/custLosts")
   public DataGridView selectAll(CustLostVo custLostVo){
     logger.debug("limit**********"+custLostVo.getLimit());
     logger.debug("curr***********"+custLostVo.getPage());
     custLostVo.getCustomer().setCustName(custLostVo.getLostReson());
     logger.debug("公司真正的名称："+custLostVo.getCustomer().getCustName());
      return customerService.selectBycondition(custLostVo);
   }

   //把客户改为暂存流失状态，也就是状态必须为暂缓而且暂缓措施必须有值(前台已经判断了)
    @MyLog(value = "修改暂存流失客户")  //这里添加了AOP的自定义注解
    @PostMapping(value = "custLostDeferUpdate.do")
   public ResultDto custLostDeferUpdate(CustLost custLost) {
        logger.debug(custLost.getLostDelay());
        logger.debug("现在的状态是" + custLost.getLostStatus());
        logger.debug("编号" + custLost.getLostNo());
        logger.debug("客户编号"+custLost.getLostCustNo());
        if (custLost.getLostStatus() == 3) {//确认流失给出提示
            return ResultDto.UPDATE_CUSTLOSTDEFER_MESSAGE;
        } else if (custLost.getLostStatus() == 1) {//正常状态从流失表中删除
            try {
                customerService.deleteLost(custLost);
                return ResultDto.REURTN_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return ResultDto.REURTN_FAILED;
            }
        } else{
            try {
                customerService.updateBycondition(custLost);
                return ResultDto.UPDATE_CUSTLOSTDEFER_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return ResultDto.UPDATE_CUSTLOSTDEFER_FAILURE;
            }
        }


   }
   @GetMapping("/customer")
   public List<Customer> allCustomer(){
      return customerService.selectAllCustomer();
   }

   //修改流失客户的流失原因或者状态
    @PostMapping("custLostConfireUpdate.do")
    @MyLog(value = "修改流失客户的流失原因或者状态")  //这里添加了AOP的自定义注解
    public ResultDto custLostConfireUpdate(CustLost custLost){
        logger.debug("状态"+custLost.getLostStatus());
        logger.debug(custLost.getLostReson());
        if(custLost.getLostStatus()==2){
            return ResultDto.UPDATE_CUSTLOSTCONFIRE_MESSAGE;
        }   else if (custLost.getLostStatus() == 1) {//正常状态从流失表中删除
            try {
                customerService.deleteLost(custLost);
                return ResultDto.REURTN_SUCCESS;
            } catch (Exception e) {
                e.printStackTrace();
                return ResultDto.REURTN_FAILED;
            }
        } else{
        try {
            customerService.updateConfirmStatus(custLost);
            return ResultDto.UPDATE_CUSTLOSTCONFIRE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.UPDATE_CUSTLOSTCONFIRE_FAILURE;
        }
    }
   }

    //查询所有客户
    @GetMapping("/customers")
    @MyLog(value = "查询所有客户")  //这里添加了AOP的自定义注解
    public DataGridView selectCustomerByPage(CustomerVo customerVo){
        logger.debug("当前页" + customerVo.getPage());
        logger.debug("每页的记录数:"+customerVo.getLimit());
        logger.debug("搜索框的信息:"+customerVo.getCustName());
       // logger.debug("地区" +customerVo.getBasicRegion().getBasiItem());
        //logger.debug("地区" +customerVo.getBasicLevel());

        return customerService.selectAllCustomers(customerVo);
    }


    //添加用户
    @MyLog(value = "添加用户")  //这里添加了AOP的自定义注解
    @PostMapping(value = "/customers.do")
    public ResultDto addCustomer(Customer customer){
        //获取当前时间，然后赋值给创建时间
        Date d=new Date();
        //时间格式
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date=timeFormat.format(d);
        customer.setCustCreateDate(date);
        Customer customerResult = this.customerService.selectCustomerByCustNo(customer.getCustNo());
        if(customerResult!=null) {
            return ResultDto.USER_EXITS;
        }else {
            boolean flag=this.customerService.addCustomer(customer);
            if (flag) {
                return ResultDto.USER_ADD_SUCCESS;
            } else {
                return ResultDto.USER_ADD_FAILURE;
            }
        }
    }
    //删除客户
    @MyLog(value = "删除客户")  //这里添加了AOP的自定义注解
    @DeleteMapping(value = "/customers/{custNo}")
    public ResultDto deleteUser(@PathVariable("custNo") String custNo){
        try {
            this.customerService.deleteCustomerByCustNo(custNo);
            return ResultDto.DELETE_USER_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_USER_FAILURE;
        }
    }
    //批量删除顾客
    @GetMapping(value = "customers/deleteBatch")
    public ResultDto deleteBathUser(@RequestParam("custNos[]") String[] custNos){
        try {
            this.customerService.removeBatchCustomer(custNos);
            return ResultDto.DELETE_USER_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_USER_FAILURE;
        }
    }

    //更新顾客信息
    @MyLog(value = "更新顾客信息")  //这里添加了AOP的自定义注解
    @PostMapping(value = "customers/update")
    public ResultDto updateCustomer(Customer customer){
        try {
            this.customerService.updateCustomer(customer);
            return ResultDto.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.UPDATE_FAILURE;
        }
    }
    //查询所有的符合流失规定的客户（6个月未下订单的客户）
    @RequestMapping(value = "/custWillLosts")
    public DataGridView selectAllWillLostCustomer(CustomerVo customerVo){
       logger.debug(customerVo.getCustName());
      return customerService.selectAllWillLostCustomer(customerVo);
    }
    //获取数组长度
    @RequestMapping(value = "/custWillLost")
    public List<Customer> selectAllWillLostCustomer(){
       return customerService.selectAllWillLostCustomer1();
    }
    //获取流失类型表格数据
    @RequestMapping(value="/custLostTable")
    public List<EnertyMy> custLostTable(){
       //返回json对象放入data中
       return enertyMyService.selectXY();

    }
    @RequestMapping(value = "/updateCustFlag")
    public ResultDto updateCustFlag(String orderCustNo){
       logger.debug("***顾客编号"+orderCustNo);
        try {
            customerService.updateCustFlag(orderCustNo);
            return ResultDto.REURTN_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.REURTN_FAILED;
        }

    }

    @RequestMapping(value = "/addCustLosts")
    public ResultDto addCustLost(String orderCustNo){
        logger.debug("***顾客编号"+orderCustNo);
        try {
            customerService.insertLost(orderCustNo);
            return ResultDto.LOST_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.LOST_FAILED;
        }

    }
}
